<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of admin_m
 *
 * @author Gidaff_01
 */
class Admin_m extends CI_Model {

    public function __construct() {
        parent::__construct();
        $this->load->database();
    }
    //--login function--//
    public function login($email, $password){
    	if($email == null && $password == null){
    		return FALSE;
    	}
    	
    	$query = $this->db->select('id, group_id, email, password')
    				      ->from('users')
    				      ->where(array('email'=>$email, 'password'=>$password))
    				      ->get();
    	if($query->num_rows() > 0){
    		$userRows = $query->row();
    		if($this->is_banned_admin($userRows->email)){
    			return FALSE;
    		}else{
    			
    			$this->last_login($userRows->id);
    			$this->session->set_userdata("user_id", $userRows->id);
    			$this->session->set_userdata("group_id", $userRows->group_id);
    			
    			//update last login time
    			
    			return TRUE; 
    			
    		}	
    	}else{
    		return FALSE;
    	}
    	
    	
    }

    //--update last login time--//
    function last_login($user_id){
    	if($user_id == null){
    		return FALSE;
    	}
    	$data = array("last_login" => now());
    	$query = $this->db->update("users", $data, array("id" => $user_id));
    	if($query){
    		return $query;
    	}else{
    		return FALSE;
    	}
    }
    
	//--check admin who is being banned--//
    public function is_banned_admin($email) {
        if ($email == '') {
            return FALSE;
        }
        return $this->db->where(array('email' => $email, 'active' => 0))
                ->count_all_results('users') == 1;
    }
    
    //-----Add new admin---//
	public function add_admin($email, $password, $additional_data = false) {
        $group_name = 'admin';
        $group_id = $this->db->select('id')
                        ->where('name', $group_name)
                        ->get('user_groups')
                        ->row()->id;

        // IP Address
        //$ip_address = $this->input->ip_address();
        
        //Password
        $pass = $password;

        // Users table.
        $data = array(
            'password' => $pass,
            'email' => $email,
            'group_id' => $group_id,
            'created_on' => now(),
            'last_login' => now(),
            'active' => 1
        );
        if ($this->db->insert('users', $data) > 0) {
            // Profile table.
            $id = $this->db->insert_id();
            $data = array('user_id' => $id);
            $columns = array('display_name', 'first_name', 'last_name', 'dob', 'gender', 'telephone number', 'mobile_number', 'address', 'avatar');
            if (!empty($columns)) {
                foreach ($columns as $input) {
                    if (is_array($additional_data) && isset($additional_data[$input])) {
                        $data[$input] = $additional_data[$input];
                    } elseif ($this->input->post($input)) {
                        $data[$input] = $this->input->post($input);
                    }
                }
            }
        }
        return $this->db->insert('user_profiles', $data) > 0;
    }
    //-----End add new admin---//
    
    //--get particular record from table--//
    
    function get_particular_record($table_name, $field, $input){
    	$query = 
	    	$this->db->select($field)
	    			 ->from($table_name)
	    			 ->where($field, $input)
	    			 ->get();
	    return $query->row();
			    			 
    }
    
    //--get admin profile information--//
    function get_admin_profiles($user_id){
    	if($user_id == null){
    		return FALSE;
    	}
    	
    	$query = $this->db->select("user_profiles.*, users.active, users.id as userid, users.group_id")
    					  ->from("user_profiles")
    					  ->join("users", "user_profiles.user_id = users.id")
    					  ->where("user_profiles.user_id", $user_id )
    					  ->limit("1")
    					  ->get();
    					  
    	if($query->num_rows() < 1){
    		return FALSE;
    	}else{
    		return $query->row();
    	}
    }

    //--update admin profile information--//
    function update_admin_profile($user_id, $data){
    	if($user_id == null){
    		return FALSE;
    	}
    	$query = $this->db->update("user_profiles", $data, array("user_id" => $user_id));
    	if($query){
    		return $query;
    	}else{
    		return FALSE;
    	}
    
    }
    
    //--update admin active status--//
    function update_admin_active($user_id, $data){
    	if($user_id == null){
    		return false;
    	}
    	$query = $this->db->update("users", $data, array("id"=>$user_id));
    	return $query;
    }
    
    //--Delete admin--//
	function delete_admin($user_id) {
        $this->db->delete('users', array('id' => $user_id));
        return $this->db->delete('user_profiles', array('user_id' => $user_id));
    }
    //--Delete multi admin--//
	function delete_admins($list) {
        $this->db->where('id in ' . $list);
        $query = $this->db->delete('users');
        $this->db->where('user_id in ' . $list);
        $query1 = $this->db->delete('user_profiles');
        return $query;
//        $this->db->delete('users', array('id' => $user_id));
//        return $this->db->delete('users_profiles', array('user_id' => $user_id));
    }
    
    //--check login--//
    function check_login($check, $input){
    	$query =$this->db->select()
    		     		 ->from("users")
    		     		 ->where($check, $input)
    		     	     ->get();
    	$count = $query->num_rows();
    	return $count;
    }
    
    
    
    
    
    
    
    
    
    
    
	//--------------------------------------------//
    

   
    
    public function update_last_login($user_id) {
        return $this->db->update('users', array('last_login' => time()), array('id' => $user_id));
    }

    public function logout() {
       /* $this->session->unset_userdata('email');
        $this->session->unset_userdata('id');*/
        $this->session->unset_userdata('user_id');
        $this->session->unset_userdata('group_id');
    }

    public function edit_profile($user_id, $data_user, $data_profile) {
        $this->db->update('users', $data_user, array('id' => $user_id));
        return $this->db->update('users_profiles', $data_profile, array('user_id' => $user_id));
    }

    public function get_admin_details($user_id) {
        $query = $this->db->select('users_profiles.*,users.active,users.group_id')
                ->from('users, users_profiles')
                ->where('users_profiles.user_id', $user_id)
                ->where('users.id = users_profiles.user_id')
                ->get();
                
        if($query->num_rows() < 1){
            return FALSE;
        }
        return $query;
    }

    public function change_pass($user_id, $password) {
        return $this->db->update('users', array('password' => $password), array('id' => $user_id));
    }

    
    public function email_check($email = '') {
        if (empty($email)) {
            return FALSE;
        }
        return $this->db->where('email', $email)
                ->count_all_results('users') > 0;
    }

    public function is_password($old_pass, $user_id) {
        return $this->db->where(array('password' => $old_pass, 'id' => $user_id))
                ->count_all_results('users');
    }

    public function get_all_admin($per_page, $off_set) {
        $this->db->select('user_profiles.display_name, users.*');
        $this->db->from('users,user_profiles');
        $this->db->where('users.id = user_profiles.user_id');
        $this->db->where('users.group_id', 2);
        $this->db->limit($per_page, $off_set);
        return $this->db->get()->result_array();
    }

    public function count_admin() {
        $this->db->select('user_profiles.*');
        $this->db->from('users,user_profiles');
        $this->db->where('users.id = user_profiles.user_id');
        $this->db->where('users.group_id', 2);
        return count($this->db->get()->result_array());
    }
    
    function get_old_password($password){
    	$query = $this->db->select()
    				      ->from("users")
    				      ->where("password", $password)
    				      ->get();	
    	$count = $query->num_rows();
    	return $count;
    			
    }

   

    

}

?>
